package com.DAO.DaoImpl;

import com.DAO.UserDao;
import com.entity.User;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public void addUser(User user) {
        try (SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()) {
            UserDao mapper = session.getMapper(UserDao.class);
            mapper.addUser(user);
            session.commit();
        }
    }

    @Override
    public List<User> getAllUsers() {
        try (SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()) {
            return session.selectList("com.DAO.UserDao.getAllUsers");
        }
    }

    @Override
    public void deleteUserById(int id) {
        try (SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()) {
            UserDao mapper = session.getMapper(UserDao.class);
            mapper.deleteUserById(id);
            session.commit();
        }
    }

    @Override
    public void updateUser(User user) {
        try (SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()) {
            UserDao mapper = session.getMapper(UserDao.class);
            mapper.updateUser(user);
            session.commit();
        }
    }
}
